<template>
  <div class="container">
    <header class="header">
      <h1>Ask Doctor</h1>
    </header>
    <main class="main-content">
      <div class="department-container">
        <ul class="department-list">
          <li
              v-for="item in departments"
              :key="item.id"
              class="department-item"
              @click="handleDepartmentClick(item)"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
    </main>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {useRouter} from "vue-router";

const departments = ref([]);
const router = useRouter();

const handleDepartmentClick = (department) => {
  axios.post(`http://localhost:8000/app/doctor_by_department/`, {id: department.id})
      .then(res => {
        console.log(res.data)
        router.push({path: department.url, query: {id: department.id,name: department.name}});
      })
  // 跳转到目标页面，并传递部门 ID 作为参数

};

onMounted(() => {
  axios.get(`http://localhost:8000/app/department/`)
      .then(res => {
        console.log(res.data);
        // 假设后端返回的数据包含所有科室及其目标路由名称
        departments.value = res.data;
      });
});
</script>

<style scoped>
/* 容器样式 */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Arial', sans-serif;
  color: #333;
  background-color: #f5f5f5;
}

/* 头部样式 */
.header {
  text-align: center;
  margin-bottom: 20px;
}

.header h1 {
  font-size: 2.5em;
  color: #333;
  margin: 0;
}

/* 主内容区域样式 */
.main-content {
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* 科室容器样式 */
.department-container {
  max-width: 800px;
  margin: 0 auto;
}

/* 科室列表样式 */
.department-list {
  list-style-type: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

/* 科室项样式 */
.department-item {
  background-color: #f0f0f0;
  padding: 15px;
  border-radius: 5px;
  text-align: center;
  font-size: 1.1em;
  color: #333;
  transition: background-color 0.3s ease, transform 0.3s ease;
  cursor: pointer;
}

.department-item:hover {
  background-color: #e0e0e0;
  transform: scale(1.05);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .container {
    padding: 10px;
  }

  .header h1 {
    font-size: 2em;
  }

  .main-content {
    padding: 15px;
  }

  .department-list {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  }

  .department-item {
    padding: 10px;
    font-size: 1em;
  }
}
</style>
